package com.chnbst.bstyun.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.Email;
import com.chnbst.bstyun.model.pojo.StaticData;
import com.chnbst.bstyun.model.vo.EmailVo;
import com.chnbst.bstyun.service.EmailService;
import com.chnbst.bstyun.service.StaticDataService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author zhangq
 */
@Controller
@RequestMapping("/sysAdmin")
public class EmailAdminController {

	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private StaticDataService staticDataService;
	@Autowired
	private EmailService emailService;
	
	/**
	 * 系统邮件设置
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/email/sysEmailManager",method=RequestMethod.GET)
	public ModelAndView sysEmailManager()throws Exception{
		return new ModelAndView("/admin/systemManager/sysEmailManager");
	}
	
	/**
	 * 添加系统邮件页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/email/addSysEmailPage",method=RequestMethod.GET)
	public ModelAndView addSysEmailPage()throws Exception{
		ModelAndView mav = new ModelAndView();
		List<StaticData> emailTypeList = staticDataService.selectStaticDataByStaticDataCode("EMAIL");
		mav.addObject("emailTypeList", emailTypeList);
		mav.setViewName("/admin/systemManager/addNewSysEmail");
		return mav;
	}
	
	/**
	 * Ajax 查询邮件列表 
	 * @return 邮件列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/email/emailList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> emailList() throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sortField", "email_id");
		map.put("sortOrder", "desc");
		List<EmailVo> emailList = emailService.selectEmailVos(map);
		map.put("emailList", emailList);
		return map;
	}
	
	/**
	 * 编辑邮件类型跳转页面
	 * @return 编辑邮件页面
	 * @throws Exception
	 */
	@RequestMapping(value="/email/updateEmail/{emailId}",method=RequestMethod.GET)
	public ModelAndView updateEmail(@PathVariable Integer emailId)throws Exception{
		ModelAndView mav = new ModelAndView();
		//加载邮件的详细信息
		Email email = emailService.selectEmail(emailId);
		List<StaticData> emailTypeList = staticDataService.selectStaticDataByStaticDataCode("EMAIL");
		mav.addObject("emailTypeList", emailTypeList);
		mav.addObject("email", email);
		mav.setViewName("/admin/systemManager/addNewSysEmail");
		return mav;
	}
	
	/**
	 * 新增,编辑邮件类型
	 * @return 邮件类型列表
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/email/addSysEmail",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addSysEmail(Email email,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSuccess = false;
		email.setIsDelete(0);
		//新增
		if(email.getEmailId() == null){
			email.setCreateTime(new Date());
			if(email.getTimeSpace()==null){
				email.setTimeSpace(0);
			}
			isSuccess = emailService.insertEmail(email);
		//编辑
		}else{
			Email emailTemp = emailService.selectEmail(email.getEmailId());
			email.setCreateTime(emailTemp.getCreateTime());
			if(email.getTimeSpace()==null){
				email.setTimeSpace(emailTemp.getTimeSpace());
			}
			isSuccess = emailService.updateEmail(email);
		}
		map.put("isSuccess", isSuccess);
		return map;
	}
	
	/**
	 * 查看邮件类型的详细信息
	 * @return 邮件详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/email/emailDetail/{emailId}",method=RequestMethod.GET)
	public ModelAndView goodsDetail(@PathVariable Integer emailId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		EmailVo email = emailService.selectEmailVo(emailId);
		
		mav.addObject("email", email);
		mav.setViewName("/admin/systemManager/emailDetail");
		
		return mav;
	}
	
	/**
	 * ajax请求 删除邮件类型
	 * @return 邮件类型列表
	 */
	@RequestMapping(value="/operate/email/deleteSysEmail",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteSysEmail(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSuccess = false;
		String emailIds = request.getParameter("ids"); 
		if(emailIds.contains(",")) {
			String[] emailId = emailIds.split(",");
			for(int i=0;i<emailId.length;i++){
				Email email = emailService.selectEmail(Integer.valueOf(emailId[i]));
				email.setIsDelete(1);
				
				isSuccess = emailService.updateEmail(email);
				map.put("message", "删除成功！！");
			}
		}else {
			Email email = emailService.selectEmail(Integer.valueOf(emailIds));
			email.setIsDelete(1);
			isSuccess = emailService.updateEmail(email);
			map.put("message", "删除成功！！");
		}
		map.put("isSuccess", isSuccess);
		return map;
	}
}